package com.joejag.mavenstats.client.views.main.projects;

import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;
import com.gwtext.client.core.EventObject;
import com.gwtext.client.widgets.Button;
import com.gwtext.client.widgets.ButtonConfig;
import com.gwtext.client.widgets.MessageBox;
import com.gwtext.client.widgets.event.ButtonListenerAdapter;
import com.joejag.mavenstats.client.MavenStats;
import com.joejag.mavenstats.client.dto.ProjectDTO;
import com.joejag.mavenstats.client.service.ProjectsService;
import com.joejag.mavenstats.client.service.ProjectsServiceAsync;
import com.joejag.mavenstats.client.views.forms.project.ProjectDTODialog;
import com.joejag.mavenstats.client.views.forms.project.edit.EditProjectDTOForm;

import java.util.List;

public class DependencyDetail extends Composite
{
  private HTML subject = new HTML();
  private HTML sender = new HTML();
  private HTML recipient = new HTML();

  private DependencyTable table = new DependencyTable();
  private ScrollPanel scroller = new ScrollPanel(table);
  private ProjectDTO currentProject;

  public DependencyDetail()
  {
    VerticalPanel detailsPanel = new VerticalPanel();
    detailsPanel.add(subject);
    detailsPanel.add(sender);
    detailsPanel.add(recipient);

    HorizontalPanel buttonsPanel = new HorizontalPanel();
    buttonsPanel.setSpacing(4);
    createEditDeleteButtons(buttonsPanel);
    buttonsPanel.setHorizontalAlignment(HorizontalPanel.ALIGN_RIGHT);

    DockPanel headerDock = new DockPanel();
    headerDock.add(detailsPanel, DockPanel.WEST);
    headerDock.add(buttonsPanel, DockPanel.EAST);
    headerDock.setWidth("100%");

    DockPanel innerPanel = new DockPanel();
    innerPanel.add(headerDock, DockPanel.NORTH);
    innerPanel.add(scroller, DockPanel.CENTER);
    innerPanel.setCellHeight(scroller, "100%");
    innerPanel.setSize("100%", "100%");

    VerticalPanel panel = new VerticalPanel();
    panel.add(innerPanel);
    scroller.setSize("100%", "100%");
    initWidget(panel);

    setStyleName("mail-Detail");

    detailsPanel.setStyleName("mail-DetailHeaderComp");
    buttonsPanel.setStyleName("mail-DetailHeaderComp");
    headerDock.setStyleName("mail-DetailHeader");

    innerPanel.setStyleName("mail-DetailInner");
    subject.setStyleName("mail-DetailSubject");
    sender.setStyleName("mail-DetailSender");
    recipient.setStyleName("mail-DetailRecipient");
  }

  private void createEditDeleteButtons(HorizontalPanel buttonsPanel)
  {
    com.gwtext.client.widgets.Button editButton = new com.gwtext.client.widgets.Button(new ButtonConfig()
    {
      {
        setText("Edit");
      }
    });
    buttonsPanel.add(editButton);
    editButton.addButtonListener(new ButtonListenerAdapter()
    {
      public void onClick(Button button, EventObject eventObject)
      {
        super.onClick(button, eventObject);
        new ProjectDTODialog(new EditProjectDTOForm(currentProject)).show(button.getEl());
      }
    });

    Button removeButton = new Button(new ButtonConfig()
    {
      {
        setText("Remove");
        setButtonListener(new ButtonListenerAdapter()
        {
          public void onClick(Button button, EventObject e)
          {
            MessageBox.confirm("Confirm", "Are you sure you want to remove " + currentProject.projectName + "?",
                new MessageBox.ConfirmCallback()
                {
                  public void execute(String btnID)
                  {
                    if (btnID.equals("yes"))
                    {
                      ProjectsServiceAsync serviceAsync = ProjectsService.App.getInstance();
                      serviceAsync.deleteProject(currentProject, new AsyncCallback()
                      {
                        public void onFailure(Throwable throwable)
                        {

                        }

                        public void onSuccess(Object o)
                        {
                          MavenStats.instance().refreshProjectsTree();
                          MavenStats.instance().showHomepage();
                        }
                      });
                    }
                  }
                });
          }
        });
      }
    });
    buttonsPanel.add(removeButton);

  }

  public void adjustSize(int windowWidth, int windowHeight)
  {
    int scrollWidth = windowWidth - scroller.getAbsoluteLeft() - 9;
    if (scrollWidth < 1)
      scrollWidth = 1;

    int scrollHeight = windowHeight - scroller.getAbsoluteTop() - 9;
    if (scrollHeight < 1)
      scrollHeight = 1;

    scroller.setSize("" + scrollWidth, "" + scrollHeight);
  }

  public void setItem(ProjectDTO project, List items)
  {
    this.currentProject = project;

    subject.setHTML(project.projectName);
    sender.setHTML("<b>Team:</b>&nbsp;" + project.teamname);
    recipient.setHTML("<b>URL:</b>&nbsp;" + project.projectURL);
    table.update(items);
  }

}
